using UnityEngine;

public class TutorialPlayer : TableTennisAIPlayer
{
	public class DesiredAim
	{
		public Vector3 desiredShootPosition = Vector3.zero;

		public float spin;
	}

	public float serveHeightAboveNet = 0.2f;

	public float floorMoveSpeed = 2f;

	public float strength = 0.5f;

	public AudioClip hitSound;

	public DesiredAim myDesiredAim;

	public override float GetSpeed(int numShotsInPoint)
	{
		return floorMoveSpeed;
	}

	public override void Init(int teamTag, int tableSide, TableTennisMatchController match, bool canAlwaysHit)
	{
		base.Init(teamTag, tableSide, match, canAlwaysHit);
	}

	protected override void ShootBall(bool isServe, KineticElementHit hit)
	{
		if (myDesiredAim != null)
		{
			myDesiredAim = ((!Table.IsPosOnTable(myDesiredAim.desiredShootPosition)) ? null : myDesiredAim);
		}
		base.ballFlyingTowardsMe = false;
		ball.HitOnServe();
		Vector3 vector = ((!isServe) ? ball.position : servePosition);
		float f = 0f;
		if (hit != null)
		{
			vector = ball.flightSequence.PositionAt(hit.flightTimeWhenHit);
			f = Mathf.Max(0f, hit.flightTimeWhenHit - ball.flightSequence.flightTime);
		}
		HitParamsDB.Hit hit2 = null;
		PlayerStatsManager.SkillType skillType = PlayerStatsManager.SkillType.Attack;
		PlayerStatsManager.SkillType skillType2 = PlayerStatsManager.SkillType.Attack;
		if (isServe)
		{
			hit2 = ScriptableObjectSingleton<HitParamsDB>.instance.GetHit(HitParamsDB.HitType.Serve);
		}
		else if (chosenHitPlan != null)
		{
			PlayerAnimationsController.HitAnimationParams hitParams = chosenHitPlan.hitParams;
			hit2 = ScriptableObjectSingleton<HitParamsDB>.instance.GetHit(hitParams.type);
		}
		Vector3 vector2 = -Vector3.forward * Table.halfLength * base.tableSide * 0.5f;
		float angularVelocityMagnitude = 0f;
		if (myDesiredAim != null)
		{
			vector2 = myDesiredAim.desiredShootPosition;
			angularVelocityMagnitude = myDesiredAim.spin;
		}
		vector2.y = 0f;
		Vector3 normalized = (vector2 - vector).OnGround().normalized;
		SetupSearch(normalized, vector);
		search.symDeltaTime = symDeltaTime;
		float num = ((!isServe) ? heightAboveNet : serveHeightAboveNet);
		float angle = 0f;
		FlightInitialConditionsSearch.MaxResult maxResult = default(FlightInitialConditionsSearch.MaxResult);
		for (int i = 0; i < 3; i++)
		{
			angle = FlightInitialConditionsSearch.GetAngle(num, 0.02f, vector);
			maxResult = search.FindMaxAcceptableVelocity(angle, angularVelocityMagnitude, FlightInitialConditionsSearch.OptimumSearchType.SearchMin, isServe);
			if (maxResult.simResult.resultClasification == FlightInitialConditionsSearch.SimResultClasification.In)
			{
				break;
			}
			num *= 2f;
		}
		FlightInitialConditionsSearch.MaxResult maxResult2 = search.FindMaxAcceptableVelocity(angle, angularVelocityMagnitude, FlightInitialConditionsSearch.OptimumSearchType.SearchMax, isServe);
		float num2 = 0f - Mathf.Sign(vector.z);
		float num3 = Mathf.Lerp(maxResult.magnitude, maxResult2.magnitude, strength);
		num3 = maxResult.magnitude + (maxResult2.magnitude - maxResult.magnitude) * strength;
		FlightParams initialConditions = search.GetInitialConditions(num3, angle, angularVelocityMagnitude);
		FlightSimulator instance = Singleton<FlightSimulator>.instance;
		FlightSimulator.SequenceSimulationParams sim = FlightSimulator.SequenceSimulationParams.DefaultRecordFlightParams(initialConditions, symFlight);
		sim.deltaTime = symDeltaTime;
		instance.FillFlightSequence(ball.flightSequence, sim);
		ball.flightSequence.Reset();
		ball.flightSequence.Update(Mathf.Abs(f));
		HitParameters hitParams2 = default(HitParameters);
		hitParams2.attackPoints = -1f;
		hitParams2.reductionFactor = 0f;
		hitParams2.opponentDefensePoints = 0f;
		PlaySound(hitSound);
		base.match.ui.ballFollower.SetColor(Color.yellow);
		ball.isServeBall = isServe;
		ball.playerThatHitBall = this;
		ball.hitParams = hitParams2;
		ball = null;
		base.match.OnPlayerHitBall(this, default(HitBallParams));
		myDesiredAim = null;
	}
}
